package comp
{
	use namespace comp_layout;

	import comp.style.css.CssAchieve;
	import comp.style.css.MergeLayout;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;

	/**
	 *<h1> label文本显示控件</h1>
	 * <h2>样式</h2>
	 * autoSize(left|right|none),fontFamily,fontSize,color,textAlign("left"|"center"|"right"),wordWrap(false|true)支持自动换行</br>
	 * paddingLeft左填充，paddingRight右填充,paddingTop上填充,paddingBottom下填充,backgroundImage,backgroundColor,backgroundRepeat
	 * 
	 * 示例:webgame.mission.MisDialog 47行,</br>webgame.mission.MisView 102行
	 * @author:pu
	 * data:2010.10.9
	 */
	public class Label extends UIComponent
	{
		include "style/BorderStyles";//包含样式
		include "style/TextStyles";//包含样式
		include "style/PaddingStyles"//内边距离
		include "style/MarginStyles"//外边距离
		include "style/SkinStyle" //皮肤属性
		include "style/Tips" //提示信息
		
		//**********样式************
		private var _wordWrap:Boolean=false;
		private var _multiline:Boolean=false;//多行文本
		///////////////////////////
		public function Label()
		{
			super(); 
			this._themeName="comp::Label";
			this.addEventListener(MouseEvent.MOUSE_OVER,onTipsMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT,onTipsMouseOut);
		}

		//********************** 属性 *************************
		public function get multiline():Boolean
		{
			return _multiline;
		}
		
		public function set multiline(value:Boolean):void
		{
			_multiline = value;
		}
		
		public function get wordWrap():Boolean
		{
			return _wordWrap;
		}

		public function set wordWrap(value:Boolean):void
		{
			_wordWrap = value;
		}

		public function get linkClickFunction():Function
		{
			return _linkClickFunction;
		}

		override public function set name(value:String):void
		{
			super.name=value;
			if(this._label)
				_label.name=value;
		}
		
		public override function set height(value:Number):void
		{
			super.height = value;
			if(this.initialized)
				merge();
		}
		
		public  override function set width(value:Number):void
		{
			super.width = value;
			if(this.initialized)
				merge();
		}
		
		/**
		 * 当点击文本中的链接时调用的函数function(e:TextEvent);
		 */
		public function set linkClickFunction(value:Function):void
		{
			_linkClickFunction = value;
		}

		public function get text():String
		{
			return _text;
		}

		public function set text(value:String):void
		{
			_text=value;
			if(initialized)
			{
				commitProperties();
				merge();
			}
		}
		
		public function get textWidth():Number
		{
			if(_label)
				return _label.textWidth;
			else
				return 0;
		}
		
		public function get textHeight():Number
		{
			if(_label)
				return _label.textHeight;
			else
				return 0;
		}
	
		
		//************* override *********************
		override public function dispose():void
		{
			super.dispose();
			_label.removeEventListener(TextEvent.LINK,linkHandler);
			_linkClickFunction=null;
			_text=null;
			_label=null;
		}
		
		override protected function createChildren():void
		{
			_label=new TextField;
			_label.selectable = true;
			_label.name=this.name;
			_label.addEventListener(TextEvent.LINK,linkHandler);
			addChild(_label);
			
			createTipsUI();
		}
		
		override protected function commitProperties():void
		{
			if(_label!=null && _text!=null)
				_label.htmlText=_text;
			
			commitTipsPropertis();
		}
		
		override protected function commitStyles():void
		{
			CssAchieve.mergeTextField(_label,this);
			if(_autoW)//		
				_label.autoSize=this.getStyle("textAlign");		
		}
		
		override protected function merge():void
		{
			MergeLayout.labelLayout(_label,this);
			CssAchieve.mergeTemplate(this);
			CssAchieve.mergeBackGrounde(this);
			this.wholeRect=new Rectangle(0,0,this.width,this.height);
		}
		
		//********************** 私有方法 *****************
		/**
		 * 链接点击事件
		 */
		protected function linkHandler(e:TextEvent):void
		{
			if(_linkClickFunction!=null)
				_linkClickFunction(e);
		}
		
		//private
		private var _label:TextField;
		private var _text:String;
		private var _linkClickFunction:Function;
		private var _offx:Number=-2;
		private var _offy:Number=-3;

	}
}